package net.sf.midollo.io.buffer;

import net.sf.midollo.io.DefaultIOStream;
import net.sf.midollo.io.IOStream;
import net.sf.midollo.io.IOStreamFactory;

import java.io.*;

public class BufferedIOStreamFactory implements IOStreamFactory {

	protected final IOStreamFactory ioStreamFactory;
	protected final int inputBufferSize;
	protected final int outputBufferSize;

	public BufferedIOStreamFactory(IOStreamFactory ioStreamFactory, int inputBufferSize, int outputBufferSize) {
		this.ioStreamFactory = ioStreamFactory;
		this.inputBufferSize = inputBufferSize;
		this.outputBufferSize = outputBufferSize;
	}

	public IOStream open() throws IOException {
		IOStream ioStream = this.ioStreamFactory.open();
		InputStream is = ioStream.getInput();
		if (this.inputBufferSize > 0)
			is = new BufferedInputStream(is, this.inputBufferSize);
		OutputStream os = ioStream.getOutput();
		if (this.outputBufferSize > 0)
			os = new BufferedOutputStream(os, this.outputBufferSize);
		return new DefaultIOStream(is, os);
	}
}
